package configuration

import (
	"encoding/json"
	"errors"
	"fmt"
	"kop/modules/configuration/client"
	"kop/util/log"
	"sort"
)

func init() {

	var err error
	if err = json.Unmarshal(client.Load("GuildLevel"), &GuildConfigs); err != nil {
		panic(err)
	}

	log.Info("Load GuildLevel.json success.")

	var m = map[int32]GuildBossConf{}
	if err = json.Unmarshal(client.Load("GuildBoss"), &m); err != nil {
		panic(err)
	}

	for _, v := range m {
		GuildBossConfigs = append(GuildBossConfigs, v)
	}
	sort.Sort(bossConfigs(GuildBossConfigs))

	for level := int32(1); level <= 10; level++ {
		if _, err = GetBoss(level); err != nil {
			panic(err)
		}
	}

	log.Info("Load GuildBoss.json success.")
}

// 建筑配置
type (
	GuildConf struct {
		Level       int32
		NeedEXP     int32
		MemberCount int32
	}

	GuildBossConf struct {
		BossID         int32  `json:"ID"` // 副本ID
		Name           string // 副本名称
		GuildLevel     int32  // 公会等级
		CostGuildMoney int32  // 开启消耗公会资金
		GuildEXP       int32  // 公会经验
		CouponGuild    int32  // 公会贡献
		KillAwardID    int32  // 奖励ID
		RandomAwardID  int32  // 奖励ID
		BossHP         int64  // 首领血量
	}
)

var (
	GuildConfigs     = map[int32]GuildConf{}
	GuildBossConfigs []GuildBossConf
)

type bossConfigs []GuildBossConf

func (c bossConfigs) Len() int           { return len(c) }
func (c bossConfigs) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c bossConfigs) Less(i, j int) bool { return c[i].BossID < c[j].BossID }

func GetByLevel(level int32) (GuildConf, error) {

	var conf, ok = GuildConfigs[level]
	if ok {
		return conf, nil
	}

	return GuildConf{}, errors.New(fmt.Sprintf("GuildLevel not found [%d]", level))
}

func GetBoss(ID int32) (GuildBossConf, error) {

	for _, v := range GuildBossConfigs {
		if v.BossID == ID {
			return v, nil
		}
	}

	return GuildBossConf{}, errors.New(fmt.Sprintf("GuildBossConf not found [%d]", ID))
}
